﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Dao;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.Utils;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.LogTool;
using Confluent.Kafka;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CastingWellEquipmentGUI.ViewModel.Modal.Launde
{
    public class LaunderBafflePanelViewModel : ViewModelBase
    {
        Control currentControl;
        /// <summary>
        /// 关闭当前窗体命令
        /// </summary>
        public DelegateCommand CloseAppCommand { get; set; }

        public DelegateCommand OperationCommand { get; set; }

       
        //等待Kafaka改变标签值结果的消息
        private IConsumer<string, object> KafkaConsumerOfChangeValue = KafkaHelper.GetConsumerChangeValue();
        public LaunderBafflePanelViewModel()
        {
            CloseAppCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CloseAppHandler)
            };
            OperationCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(OperationHandler)
            };
        
        }



        /// <summary>
        /// 退出程序命令处理程序
        /// </summary>
        /// <param name="obj">参数</param>
        private void CloseAppHandler(object obj)
        {
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideLaunderBafflePanel"));
        }


        private void OperationHandler(object obj)
        {
            ControlAndWindowTag controlAndWindowTagWhere = new ControlAndWindowTag();
            controlAndWindowTagWhere.ControlId = currentControl.ControlId;
            controlAndWindowTagWhere.WindowName = "LaunderBafflePanel";
            controlAndWindowTagWhere.WindowControlName = obj.ToString();
            string sql = @"SELECT *
                          FROM DB_CWEGUI.dbo.ControlAndWindowTag
                          WHERE ControlId = @ControlId
                          AND WindowName = @WindowName
                          AND WindowControlName = @WindowControlName";
            int TagId = DapperToolsThisSystem.Query(sql, controlAndWindowTagWhere).TagId;
            SetTagValueMsg setTagValueMsg = new SetTagValueMsg
            {
                TagId = TagId,
                TagValue = "true"
            };
            string jsonStr = JsonConvert.SerializeObject(setTagValueMsg);
            KafkaHelper.ProduceMessage(jsonStr);
            int WaitingTime = 0;
            while (true)
            {
                Thread.Sleep(100);
                if (ThreadController.RoundBarLaunderThreadState)
                {
                    //开始消费
                    var result = KafkaConsumerOfChangeValue.Consume();

                    if (result.Message.Value != null)
                    {
                        //手动提交
                        KafkaConsumerOfChangeValue.Commit(result);
                        //获取消息内容
                        string resultValue = result.Message.Value.ToString();
                        if (resultValue == "SUCCESS")
                        {
                            Messenger.Default.Send<string>(currentControl.ControlName, "LaunderPageRefreshControl");
                            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideLaunderBafflePanel"));
                        }
                        break;
                    }
                    else
                    {
                        Thread.Sleep(900);
                        WaitingTime++;
                    }
                    if (WaitingTime == 2)
                    {
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 根据窗体名称和控件名称加载当前控制的标签
        /// </summary>
        /// <param name="framName"></param>
        /// <param name="controlName"></param>
        public void LoadTagInfoByFramNameAndControlName(object framName, object controlName)
        {
            for (int i = 0; i < LogTool.CurrentUser.Roles.Count; i++)
            {
                for (int j = 0; j < LogTool.CurrentUser.Roles[i].Permissions.Count; j++)
                {
                    if(LogTool.CurrentUser.Roles[i].Permissions[j].PForm != null &&
                        LogTool.CurrentUser.Roles[i].Permissions[j].PControl != null)
                    {
                        if (LogTool.CurrentUser.Roles[i].Permissions[j].PForm.FormName == framName.ToString() &&
                                               LogTool.CurrentUser.Roles[i].Permissions[j].PControl.ControlName == controlName.ToString())
                        {
                            currentControl = LogTool.CurrentUser.Roles[i].Permissions[j].PControl;
                            return;
                        }
                    }

                }
            }
        }
    }
}
